home *** CD-ROM | disk | FTP | other *** search
- /* Handle high-level events.
-
- 93/11/15 aih
- - for compatability with prior system software, added functions for
- opening and printing a single file pointer without using the Apple event
- manager
-
- 93/10/20 aih
- - added functions to open or print a list of files
-
- 93/10/17 aih
- - made handlers more generic by allowing application to install its own
- handlers
-
- 93/03/19 AIH
- - Moved pre-system 7.0 file open/print to ApplicationLib.c
-
- 93/03/10 Ari Halberstadt (AIH)
- - Created */
-
- #include <AppleEvents.h>
- #include "EventLib.h"
- #include "FileLib.h"
- #include "HighLevelEventLib.h"
- #include "MacLib.h"
-
- typedef struct {
- void (*handler)(...);
- void *data;
- } HighLevelType;
-
- static HighLevelType handlers[HL_LAST];
-
- void HLEventInstall(HighLevelKind kind, void (*handler)(...),
- void *data)
- {
- require(HL_FIRST <= kind && kind < HL_LAST);
- handlers[kind].handler = handler;
- handlers[kind].data = data;
- }
-
- void HLEventOpen(AEDescList *list, long n)
- {
- if (handlers[HL_OPEN].handler)
- handlers[HL_OPEN].handler(list, n, handlers[HL_OPEN].data);
- }
-
- void HLEventOpenOne(FileType *fp)
- {
- if (handlers[HL_OPEN_ONE].handler)
- handlers[HL_OPEN_ONE].handler(fp, handlers[HL_OPEN_ONE].data);
- }
-
- void HLEventPrint(AEDescList *list, long n)
- {
- if (handlers[HL_PRINT].handler)
- handlers[HL_PRINT].handler(list, n, handlers[HL_PRINT].data);
- }
-
- void HLEventPrintOne(FileType *fp)
- {
- if (handlers[HL_PRINT_ONE].handler)
- handlers[HL_PRINT_ONE].handler(fp, handlers[HL_PRINT_ONE].data);
- }
-
- void HLEventOpenApplication(void)
- {
- if (handlers[HL_OAPP].handler)
- handlers[HL_OAPP].handler(handlers[HL_OAPP].data);
- }
-
- void HLEventQuit(void)
- {
- if (handlers[HL_QUIT].handler)
- handlers[HL_QUIT].handler(handlers[HL_QUIT].data);
- EventLoopExit(true); /* handler can raise an exception to prevent quitting */
- }
-
- /* count number of files in list of files */
- long AECountFiles(AEDescList *list)
- {
- long n;
-
- FailOSErr(AECountItems(list, &n));
- return(n);
- }
-
- /* get n'th file in list of files */
- void AEGetNthFile(AEDescList *list, long n, FileType *fp)
- {
- AEKeyword keywd;
- Size actsize;
- DescType type;
- FSSpec fspec;
-
- FailOSErr(AEGetNthPtr(list, n, typeFSS, &keywd, &type,
- (Ptr) &fspec, sizeof(fspec), &actsize));
- FileSetFSSpec(fp, &fspec);
- }
-
- void AEGotRequiredParameters(AppleEvent *event)
- {
- DescType type;
- Size actsize;
- OSErr err = noErr;
-
- err = AEGetAttributePtr(event, keyMissedKeywordAttr, typeWildCard,
- &type, NULL, 0, &actsize);
- if (! err)
- err = errAEEventNotHandled;
- else if (err == errAEDescNotFound)
- err = noErr;
- FailOSErr(err);
- }
-
- static OSErr DoAEOpenOrPrint(AppleEvent *event,
- void (*action)(AEDescList *list, long n))
- {
- volatile AEDescList list;
- volatile Boolean gotlist = false;
- OSErr err = noErr;
-
- TRY {
- FailOSErr(AEGetParamDesc(event, keyDirectObject, typeAEList, &list)); gotlist = true;
- AEGotRequiredParameters(event);
- action(&list, AECountFiles(&list));
- } CLEANUP {
- if (gotlist) AEDisposeDesc(&list);
- } CATCH {
- err = FailReason();
- NOPROPAGATE;
- } ENDTRY;
- return(err);
- }
-
- static pascal OSErr HandleAEOpen(AppleEvent *event,
- AppleEvent *reply, long refcon)
- {
- return(DoAEOpenOrPrint(event, HLEventOpen));
- }
-
- static pascal OSErr HandleAEPrint(AppleEvent *event,
- AppleEvent *reply, long refcon)
- {
- return(DoAEOpenOrPrint(event, HLEventPrint));
- }
-
- static pascal OSErr HandleAEQuit(AppleEvent *event,
- AppleEvent *reply, long refcon)
- {
- OSErr err = noErr;
-
- TRY {
- AEGotRequiredParameters(event);
- HLEventQuit();
- } CATCH {
- err = FailReason();
- NOPROPAGATE;
- } ENDTRY;
- return(err);
- }
-
- static pascal OSErr HandleAEOpenApplication(AppleEvent *event,
- AppleEvent *reply, long refcon)
- {
- OSErr err = noErr;
-
- TRY {
- AEGotRequiredParameters(event);
- HLEventOpenApplication();
- } CATCH {
- err = FailReason();
- NOPROPAGATE;
- } ENDTRY;
- return(err);
- }
-
- void HLEventInit(void)
- {
- if (MacHasAppleEvents()) {
- FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, HandleAEOpenApplication, 0, false));
- FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, HandleAEOpen, 0, false));
- FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, HandleAEPrint, 0, false));
- FailOSErr(AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, HandleAEQuit, 0, false));
- }
- }
-